/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.casestudy1.view;

import com.casestudy1.main.MainMidlet;
import com.sun.lwuit.Button;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.ComponentGroup;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.table.TableLayout;

/**
 *
 * @author Windsea
 */
public class LoginForm extends View {
    boolean isLocal = true;
    private Command exitCommand;
    private Command backCommand;
    private Command aboutCommand;
    private Command optionCommand;
    private Label lbUsername;
    private Label lbPassword;
    private TextField txtUsername;
    private TextField txtPassword;
    private String username;
    private String password;
    private CheckBox ckb1;
    private Label txta;
    final ComponentGroup grp = new ComponentGroup();
    
    public LoginForm(final MainMidlet midlet) {
        super(midlet);
        setTitle("Login");

//Set layout 
        TableLayout layout = new TableLayout(4, 2);
        setLayout(layout);



//        setTransitionInAnimator(CommonTransitions.createSlide(
//                CommonTransitions.SLIDE_HORIZONTAL, false, 1000));
        setTransitionOutAnimator(CommonTransitions.createSlide(
                CommonTransitions.SLIDE_HORIZONTAL, false, 1000));
        
        Button btn1 = new Button("Logon");
        btn1.getUnselectedStyle().setAlignment(Label.CENTER);
        btn1.getSelectedStyle().setAlignment(Label.CENTER);
        Button btn2 = new Button("Register");
        btn2.getUnselectedStyle().setAlignment(Label.CENTER);
        btn2.getSelectedStyle().setAlignment(Label.CENTER);
        
        lbUsername = new Label("Username");
        
        lbPassword = new Label("Password");
        txtUsername = new TextField();
        txtPassword = new TextField();
        txtPassword.setConstraint(TextField.PASSWORD);
        
        ckb1 = new CheckBox("Remote Database");
        ckb1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (ckb1.isSelected()) {
                    isLocal = false;
                }else{
                    isLocal = true;
                }
            }
        });
        TableLayout.Constraint constraint = layout.createConstraint();
        constraint.setHorizontalSpan(2);
        constraint.setHeightPercentage(20);
        txta = new Label("Please fill out the information below completely.");
        
        txta.startTicker(UIManager.getInstance().getLookAndFeel().getTickerSpeed(), true);
        
        addComponent(constraint, txta);
        
        addComponent(lbUsername);
        addComponent(txtUsername);
        addComponent(lbPassword);
        addComponent(txtPassword);
        constraint = layout.createConstraint();
        constraint.setHorizontalSpan(2);
//        addComponent(constraint, ckb1);

        exitCommand = new Command("Exit") {
            public void actionPerformed(ActionEvent evt) {
                Display.getInstance().exitApplication();
            }
        };
        
        aboutCommand = new Command("About") {
            public void actionPerformed(ActionEvent evt) {
                midlet.showAboutForm();
            }
        };
        
        optionCommand = new Command("Setting") {
            public void actionPerformed(ActionEvent evt) {
                midlet.showOptionForm();
            }
        };
        backCommand = new Command("") {
            public void actionPerformed(ActionEvent evt) {
                Display.getInstance().exitApplication();
            }
        };
        
        
        addComponent(btn1);
        addComponent(btn2);
        addCommand(exitCommand);
        addCommand(aboutCommand);
        addCommand(backCommand);
        addCommand(optionCommand);
        setBackCommand(backCommand);

        //actionlistener
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if(isLocal){
                password = midlet.getUser(txtUsername.getText());
                username = txtUsername.getText();
                } else {
                    Dialog.show("", "Connecting to server", "OK", null);
                }
//                System.out.println("ádf"+username);
                if (txtUsername.getText().equals("")) {
                    Dialog.show("Warning!!", "Username is not filled", Dialog.TYPE_WARNING, null, "I see", null);
                } else if (txtPassword.getText().equals("")) {
                    Dialog.show("Cảnh báo", "Password is not filled", Dialog.TYPE_WARNING, null, "I see", null);
                } else if (txtPassword.getText().equals(password)) {
                    Dialog.show("System Info", "Login successfully", null, null, Dialog.TYPE_INFO, null, 1000);
//                    a.showPopupDialog(new TextField("ABCmmmmmm"));
                    midlet.createViews2();
                    midlet.showMainMenu();
//                    txtUsername.setText("");
//                    txtPassword.setText("");
                } else {
                    Dialog.show("Warning", "Wrong username hoặc password.", Dialog.TYPE_WARNING, null, "Okay :(", null);
                }
            }
        });
        
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
//                midlet.showRegisterForm();
                midlet.showRegisterForm();
            }
        });
        
        grp.addComponent(ckb1);
        addComponent(constraint, grp);
        ckb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                if (ckb1.isSelected()) {
                    TextField destination = new TextField(midlet.getIP());
                    destination.setFocus(false);
                    if (ckb1.getUIID().startsWith(grp.getElementUIID())) {
                        destination.setUIID(ckb1.getUIID());
                    }
                    grp.replaceAndWait(ckb1, destination,
                            CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 200), 1000);
                    grp.replaceAndWait(destination, ckb1,
                            CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, 200), 10000);
                    
                }
            }
        });
        
    }
    
    public boolean getIsLocal() {
        return isLocal;
    }
}
